﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.MissionViews.MissionAgentContourControllerView
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System;
using System.Collections.Generic;
using TaleWorlds.Engine;
using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.MissionViews
{
  public class MissionAgentContourControllerView : MissionView
  {
    private const bool IsEnabled = false;
    private uint _nonFocusedContourColor = new Color(0.85f, 0.85f, 0.85f).ToUnsignedInteger();
    private uint _focusedContourColor = new Color(1f, 0.84f, 0.35f).ToUnsignedInteger();
    private uint _friendlyContourColor = new Color(0.44f, 0.83f, 0.26f).ToUnsignedInteger();
    private List<Agent> _contourAgents;
    private Agent _currentFocusedAgent;
    private bool _isContourAppliedToAllAgents;
    private bool _isContourAppliedToFocusedAgent;
    private bool _isMultiplayer;

    private bool _isAllowedByOption => !BannerlordConfig.HideBattleUI || GameNetwork.IsMultiplayer;

    public MissionAgentContourControllerView()
    {
      this._contourAgents = new List<Agent>();
      this._isMultiplayer = GameNetwork.IsSessionActive;
    }

    public override void OnMissionScreenTick(float dt)
    {
      base.OnMissionScreenTick(dt);
      if (!this._isAllowedByOption)
        return;
      int num = NativeConfig.GetUIDebugMode ? 1 : 0;
    }

    private void PopulateContourListWithAgents()
    {
      this._contourAgents.Clear();
      if (this.Mission?.PlayerTeam?.PlayerOrderController == null)
        return;
      foreach (Formation selectedFormation in (List<Formation>) Mission.Current.PlayerTeam.PlayerOrderController.SelectedFormations)
        selectedFormation.ApplyActionOnEachUnit((Action<Agent>) (agent =>
        {
          if (agent.IsMainAgent)
            return;
          this._contourAgents.Add(agent);
        }));
    }

    public override void OnFocusGained(
      Agent agent,
      IFocusable focusableObject,
      bool isInteractable)
    {
      base.OnFocusGained(agent, focusableObject, isInteractable);
      int num = this._isAllowedByOption ? 1 : 0;
    }

    public override void OnFocusLost(Agent agent, IFocusable focusableObject)
    {
      base.OnFocusLost(agent, focusableObject);
      if (!this._isAllowedByOption)
        return;
      this.RemoveContourFromFocusedAgent();
      this._currentFocusedAgent = (Agent) null;
    }

    private void AddContourToFocusedAgent()
    {
      if (this._currentFocusedAgent == null || this._isContourAppliedToFocusedAgent)
        return;
      this._currentFocusedAgent.AgentVisuals?.SetContourColor(new uint?(this._focusedContourColor));
      this._isContourAppliedToFocusedAgent = true;
    }

    private void RemoveContourFromFocusedAgent()
    {
      if (this._currentFocusedAgent == null || !this._isContourAppliedToFocusedAgent)
        return;
      if (this._contourAgents.Contains(this._currentFocusedAgent))
        this._currentFocusedAgent.AgentVisuals?.SetContourColor(new uint?(this._nonFocusedContourColor));
      else
        this._currentFocusedAgent.AgentVisuals?.SetContourColor(new uint?());
      this._isContourAppliedToFocusedAgent = false;
    }

    private void ApplyContourToAllAgents()
    {
      if (this._isContourAppliedToAllAgents)
        return;
      foreach (Agent contourAgent in this._contourAgents)
      {
        uint num = contourAgent == this._currentFocusedAgent ? this._focusedContourColor : (this._isMultiplayer ? this._friendlyContourColor : this._nonFocusedContourColor);
        contourAgent.AgentVisuals?.SetContourColor(new uint?(num));
      }
      this._isContourAppliedToAllAgents = true;
    }

    private void RemoveContourFromAllAgents()
    {
      if (!this._isContourAppliedToAllAgents)
        return;
      foreach (Agent contourAgent in this._contourAgents)
      {
        if (this._currentFocusedAgent == null || contourAgent != this._currentFocusedAgent)
          contourAgent.AgentVisuals?.SetContourColor(new uint?());
      }
      this._isContourAppliedToAllAgents = false;
    }
  }
}
